<?php
class App_Sign
{
    
    // 签到操作
    public static function handle_sign($uid){
        //首先查询这个用户是否已经签到过
        $query = DB::select()
                ->from('huiyuan_sign')
                ->where('uid', '=', $uid)
                ->where('date', '=', date('Y-m-d', time()))
                ->execute()
                ->as_array();
        if(!empty($query))
             return array('errorcode' => 'SUCC');
        
        //将签到结果插入到数据库
        list($insert_id, $rows) = DB::insert('huiyuan_sign')
            ->columns (array('uid', 'date'))
            ->values (array($uid, date('Y-m-d', time())))
            ->execute();

        return array('errorcode' => 'SUCC');
    }
    
    // 查询该用户总共签到了多少次
    public static function get_total_sign_num($uid)
    {
        $query = DB::select()
                ->from('huiyuan_sign')
                ->where('uid', '=', $uid)
                ->execute()
                ->as_array();
        return count($query);
        
    }
            
    // 查询用户连续签到了多少次
    public static function get_continuity_sign_num($uid)
    {
        $count = 0;
        $today = date('Y-m-d');
        $yestoday = date('Y-m-d', strtotime("-1 day"));
        while (TRUE){
            if(self::is_sign($uid, $yestoday))
            {
                $count = $count + 1;
                $yestoday = date('Y-m-d', strtotime($yestoday)-86400 );
            }else{
                break;
            }
        }
        
        if(self::is_sign($uid, $today)){
            $count = $count + 1;
        }
        return $count;
        
    }
    
    // 获取用户签到记录
    public static function get_sign_list($uid, $start_day = null, $end_day = null)
    {
        if(empty($start_day) || empty($end_day))
        {
            $start_day = date('Y-m-d', time()-30*86400);
            $end_day = date('Y-m-d');
        }
        
        $query = DB::select('date')
                ->from('huiyuan_sign')
                ->where('uid', '=', $uid)
                ->where('date', '>=', $start_day)
                ->where('date', '<=', $end_day)
                ->execute()
                ->as_array();
        
        $arr = array();
        foreach ($query as $v) {
            $arr[] = $v['date'];
        }
        
        return $arr;
    }
    
    
    // 获取用户连续签到记录
    public static function get_continuity_sign_list($uid, $start_day = null, $end_day = null)
    {
        if(empty($start_day) || empty($end_day))
        {
            $start_day = date('Y-m-d', time()-30*86400);
            $end_day = date('Y-m-d');
        }
        
        $arr = array();
        $today = date('Y-m-d');
        while (TRUE){
            if(self::is_sign($uid, $today))
            {
                $arr[] = $today;
                $today = date('Y-m-d', strtotime($today)-86400 );
            }else{
                break;
            }
        }
        
        return $arr;
    }


    // 判断用户昨天是否签到
    public static function is_sign($uid, $date)
    {
        $query = DB::select()
                ->from('huiyuan_sign')
                ->where('uid', '=', $uid)
                ->where('date', '=', $date)
                ->limit(1)
                ->execute()
                ->as_array();
        if(empty($query)){
            return FALSE;
        }else{
            return TRUE;
        }
    }


    //判读用户是否该获得奖品
    public static function check_prize()
    {
        
    }
    
    
}